/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.serv.shop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.common.utils.BusinessUtils;
import com.jnzc.ocr.common.utils.DateUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.dependencies.serv.utils.MybatisUtils;
import com.jnzc.ocr.serv.shop.consts.ShopDictConsts;
import com.jnzc.ocr.serv.shop.entity.ShopSupplier;
import com.jnzc.ocr.serv.shop.entity.ShopSupplier;
import com.jnzc.ocr.serv.shop.entity.ShopSupplierCategory;
import com.jnzc.ocr.serv.shop.filter.ShopSupplierFilter;
import com.jnzc.ocr.serv.shop.filter.ShopSupplierFilter;
import com.jnzc.ocr.serv.shop.mapper.ShopSupplierMapper;
import com.jnzc.ocr.serv.shop.service.ShopSupplierCategoryService;
import com.jnzc.ocr.serv.shop.service.ShopSupplierService;
import com.jnzc.ocr.serv.system.api.SysDictApi;
import com.jnzc.ocr.serv.system.vo.DictPair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/13
 */
@Service
public class ShopSupplierServiceImpl implements ShopSupplierService {

    @Autowired
    private ShopSupplierMapper shopSupplierMapper;

    @Autowired
    private ShopSupplierCategoryService shopSupplierCategoryService;

    @Autowired
    private SysDictApi sysDictApi;

    @Override
    public ShopSupplier find(Long id, Long shopId) {
        ValidateUtils.isId(id, "供应商ID");
        ShopSupplier filter = new ShopSupplier();
        filter.setId(id);
        filter.setShopId(shopId);
        ShopSupplier obj = shopSupplierMapper.selectOne(filter);
        BusinessUtils.notNull(obj, "供应商信息为空");
        return obj;
    }

    @Override
    public PageResult<ShopSupplier> search(ShopSupplierFilter filter, Integer pageNo, Integer pageSize) {
        Pagination pagination = MybatisUtils.createPagination(pageNo, pageSize);
        EntityWrapper<ShopSupplier> ew = createEntityWrapper(filter);
        return MybatisUtils.createPageResult(shopSupplierMapper.selectPage(pagination, ew), pagination);
    }

    @Override
    public ShopSupplier save(ShopSupplier obj) {
        ValidateUtils.isId(obj.getShopId(), "店铺ID");
        ValidateUtils.notEmpty(obj.getName(), 20, "名称");
        ValidateUtils.notNull(obj.getAccessType(), "获取方式");
        ValidateUtils.notNull(obj.getSupplierCategoryId(), "供应商分类ID");
        if(null == obj.getAccessTypeName()){
            DictPair dictPair = sysDictApi.findPair(ShopDictConsts.SHOP_SUPPLIER_ACCESS_TYPE, obj.getAccessType());
            obj.setAccessTypeName(dictPair.getName());
        }
        if(null == obj.getSupplierCategoryName()){
            ShopSupplierCategory supplierCategory = shopSupplierCategoryService.find(obj.getSupplierCategoryId(), obj.getShopId());
            obj.setSupplierCategoryName(supplierCategory.getName());
        }
        if(null == obj.getStockAmount()){
            obj.setStockAmount(BigDecimal.ZERO);
        }
        if(null == obj.getArrearAmount()){
            obj.setArrearAmount(BigDecimal.ZERO);
        }
        if(null == obj.getId()){
            obj.setCreateTime(DateUtils.getNow());
            shopSupplierMapper.insert(obj);
        } else {
            shopSupplierMapper.updateById(obj);
        }
        return obj;
    }

    @Override
    public void delete(Long id) {
        shopSupplierMapper.deleteById(id);
    }

    private EntityWrapper<ShopSupplier> createEntityWrapper(ShopSupplierFilter filter) {
        ShopSupplier entity = new ShopSupplier();
        entity.setShopId(filter.getShopId());
        EntityWrapper<ShopSupplier> ew = new EntityWrapper<ShopSupplier>(entity);
        MybatisUtils.fillEntityWrapper(ew, filter);
        return ew;
    }
}
